


use  ENOE_final07, clear 
	
**** SAMPLE SELECTION  *******
	
	
	* Keep only HH with successfull interviews at first visit 
	
		bys id_h: egen min_HH_ent= min(N_ENT)  
		keep if min_HH_ent==1
	
	* RURAL AREA only
		g ruralarea      = T_LOC == 4 | T_LOC == 3
		tempvar t
		g `t'= ruralarea if N_ENT==1
		bys id_h : egen rurper1 = max(`t')
		
		keep if rurper1 == 1
		
	* Attrition *
	
	* 2% indiv per quarter out-migrate from origin hh 
	* 20% attrition after 5 quarters
	
	bys id: egen max_ent= max(N_ENT) 	 
	bys id_h: egen max_HH_ent= max(N_ENT) 
	ta panel_group	max_ent if N_ENT==1 , row
	
	*household level : 14% after 5 quarters	 
	bys id_h PER : gen hh= _n==1
	ta panel_group N_ENT  
	ta panel_group N_ENT  if hh==1
	ta panel_group	max_HH_ent if N_ENT==1 & hh==1, row
		
		
	* AGE  > 14 and < 75 
		
		keep if age >=14 
		keep if age<=75
		
		bys id: g ii= _n==1
	

/* Household characteritics */ 

		destring ENT, gen(state) 
			
		bys id_h PER : egen hhsize =total(1)
		bys id_h PER : egen nb_child5 =total( age<=5)  
		bys id_h PER : egen nb_child614=total( age>=6 & age<=14 )  
		bys id_h PER : egen nb_adu=total( age>=14 ) 
		bys id_h PER : egen nb_65plus =total( age>=66)  
		
		* max educa in hh 
		
		bys id_h PER :egen hh_maxyedu = max( yedu) 
		
	
* WORK 

	 g  workLW = work_lastweek
	 g  noagr_w=  (workLW==1  )* (typework==1) * (typesector==2)   if  work_lastweek!=. 
	 g  agr_w=  (workLW==1  )* (typework==1) * (typesector==1)   if  work_lastweek!=. 
	 
	 g self_noagr =  (workLW==1  )* (typework==2|typework==3|typework==4 ) * (typesector==2) if  work_lastweek!=. 
	 g self_agr =  (workLW==1  )* (typework==2|typework==3|typework==4 ) * (typesector==1) if  work_lastweek!=. 
	
	g  emp = 1 if noagr_w==1
	replace emp= 2 if  agr_w==1
	replace emp= 3 if  self_noagr ==1
	replace emp= 4 if   self_agr ==1
	replace emp= 0 if workLW==0 
	
	label def emplo 0 "non-working" 1 "job noagri" 2 "job agri" 3 "self noagri" 4 "self agri"	
	label values emp emplo
	
	label var  workLW "work last week"
	label var noagr_w "non-agri jobs"
	label var agr_w "agri jobs"
	label var self_noagr "non-agri self"
	label var  self_agr "agri self"
	
	
	foreach v in workLW noagr_w agr_w self_noagr self_agr   {
	
		g `v'_hour = hoursweek * `v' 
		replace  `v'_hour = . if `v' ==1 & ( hoursweek==0 | hoursweek==.) 
	}
	*
	
	
	****
	
	replace labincome_month = P6B2 
	

* LABOR INCOME

 replace labincome_month= min(labincome_month, 100000) if labincome_month!=.
 replace labincome_month=0  if 	 workLW==0
 
 g mis_inc= labincome_month==.
 replace mis_inc=0 if 	 workLW==0
	tempvar t 
	g `t' = mis_inc if workLW==1
	bys id_h PER : egen hhmis= max(`t')
	bys id_h PER : egen hhmis_all= min(`t')
	
	bys id_h PER :egen hhlabincome= total(labincome_month)
	replace hhlabincome=. if hhmis_all==1
	g  hhlabinc_percap = hhlabincome/hhsize
	
		
*** 	MIGRATION from q to q +1  ***		
		
		*us migrant =1 in quarter q if migrate between q and q+1
					
	bys id_h PER : egen HHusmig = max( usmigrant)
	bys id_h PER : egen HHmexmig  = max( mexmigrant)
	
	
	* DROP OBSERVATIONS THAT ARE TREATET AFTER US migration
		
		tempvar t
	    g `t' =N_ENT if HHusmig==1
		bys id_h : egen min_t_us = min (`t')
		
		drop if  N_ENT > min_t_us & min_t_us! =. 
		
		bys id_h  : egen HHusmig_ever = max( HHusmig) 
	
		
	
	/* DYNAMIC TREATMENT */ 
	
	
		foreach q in 1 2 3 4 {
		
		local q1 = `q'+1 
		
		tempvar t
		g `t' = HHusmig  if N_ENT==`q'
		bys id_h : egen HHUS_q`q'`q1' = max(`t')
			}
		*
		
	
	foreach dest in  US {	
		
		g       HH`dest'_A4 =  HH`dest'_q45  if N_ENT==1 
		replace HH`dest'_A4 = 0 if N_ENT >= 2 
		
		g       HH`dest'_A3 =  HH`dest'_q34  if N_ENT==1 
		replace HH`dest'_A3 =  HH`dest'_q45  if N_ENT==2
		replace HH`dest'_A3 = 0 if N_ENT >= 3 
		
		g       HH`dest'_A2 =  HH`dest'_q23  if N_ENT==1 
		replace HH`dest'_A2 =  HH`dest'_q34  if N_ENT==2
		replace HH`dest'_A2 =  HH`dest'_q45  if N_ENT==3
		replace HH`dest'_A2 = 0 if N_ENT >= 4
		
		g       HH`dest'_A1 =  HH`dest'_q12  if N_ENT==1 
		replace HH`dest'_A1 =  HH`dest'_q23  if N_ENT==2
		replace HH`dest'_A1 =  HH`dest'_q34  if N_ENT==3
		replace HH`dest'_A1 =  HH`dest'_q45  if N_ENT==4
		replace HH`dest'_A1 = 0 if N_ENT >= 5
		
	}
	*
	   	
	foreach dest in   US  {
	    
		label var HH`dest'_A4 "HHmig in `dest' q+3 - q+4"
		label var HH`dest'_A3 "HHmig in `dest' q+2 - q+3"
		label var HH`dest'_A2 "HHmig in `dest' q+1 - q+2 "
		label var HH`dest'_A1 "HHmig in `dest' q-q+1"
	
	}
	*
	
	
		
		g age_sq = age^2
		g yedu_sq = yedu^2
		
		g yedu_ = yedu if  N_ENT ==1
		g  age_   = age if  N_ENT ==1
			
		bys id : egen age_q1= max(age )
		bys id : egen yedu_q1= max(yedu_ )
		
		g age_sq_q1 = age_q1^2
		g yedu_sq_q1 = yedu_q1^2
		
		bys id_h : egen hhsize_q1 = max(hhsize*(N_ENT==1) )
		bys id_h : egen nbchild_q1 = max((nb_child5+ nb_child614) *(N_ENT==1) )
		bys id_h : egen nbold_q1 =max((nb_65plus) *(N_ENT==1) ) 
		bys id_h : egen hhmaxedu_q1 =max( hh_maxyedu  *(N_ENT==1) ) 
		
	
		egen id_mun = group( ENT MUN )  
		egen id_period = group( PER)
		
		egen id_mun_period = group( id_mun id_period) 
		
		egen id_indiv = group(id) 
		egen id_household = group(id_h) 
		 
	global hh_char   hhsize_q1  nbchild_q1 nbold_q1 hhmaxedu_q1
	global indiv    male age age_sq yedu yedu_sq 
	global hh_char   hhsize   nbchild nbold  hhmaxedu
	
	
	**** keep  household with at least 2 observations over time ***
	
		tempvar h
		bys id_h PER : g `h'= _n==1
		bys id_h: egen nb_hh_survey = total(`h')
	     keep if nb_hh_survey >=2
	
	cap drop hh
	bys id_h PER: g hh=_n==1
	
 
		
	
********************************************************************************	
**   PRE-TREND  FIGURE A.1 
****************************************************************************************
	
	global option varlabels  tex plain fragment  bdec(3)  se   starloc(1) starlevels(10 5  1) 
	global treatall HHUS_A4  HHUS_A3  HHUS_A2   HHUS_A1 
	
save temp, replace
	
	use temp,clear 
	
	outreg, clear(pretrend) 
	
	foreach v in  workLW noagr_w agr_w self_noagr self_agr   {
	 
			local a : variable label  `v'
		
		* WITHOUT INDIV FIXED EFFECTS
		
		qui reghdfe `v'   ${treatall}  ${hh_char} ${indiv} if age<=75  , absorb(id_mun id_period  ) vce (cluster id_household )
		
		outreg,  nod merge(pretrend)  keep( ${treatall}   )    ctitle(" ", "`v'")  ${option}
		di "`v'"
		*test HHUS_A4 = HHUS_A1 
			foreach i in 1 2 3 4{
			cap drop  b_par_`v'_`i' 
			cap drop se_par_`v'_`i'
			g b_par_`v'_`i'  = _b[HHUS_A`i'] 
			g se_par_`v'_`i'  = _se[HHUS_A`i']
			}
			
		qui reghdfe `v'_hour   ${treatall} ${hh_char} ${indiv} if age<=75    , absorb(id_mun id_period   ) vce (cluster id_household )
		outreg,   nod merge(pretrend)  keep(${treatall}   )    ctitle(" ", "hour `v' ")  ${option}
		di "`v'" "hour"
		*test HHUS_A4 = HHUS_A1 
			foreach i in 1 2 3 4{
			cap drop  b_hour_`v'_`i' 
			cap drop se_hour_`v'_`i'
			g b_hour_`v'_`i'  = _b[HHUS_A`i'] 
			g se_hour_`v'_`i'  = _se[HHUS_A`i'] 
			}
	 }
	 *

		
		keep b_* se_*
		keep if _n==1
		g e=100000
		
		reshape long ///
		b_par_workLW_  b_par_noagr_w_  b_par_agr_w_  b_par_self_noagr_  b_par_self_agr_  ///
		b_hour_workLW_ b_hour_noagr_w_  b_hour_agr_w_  b_hour_self_noagr_  b_hour_self_agr_  ///
		se_par_workLW_ se_par_noagr_w_  se_par_agr_w_  se_par_self_noagr_  se_par_self_agr_  ///
		se_hour_workLW_ se_hour_noagr_w_  se_hour_agr_w_  se_hour_self_noagr_  se_hour_self_agr_  ///
		, i(e) j(time) 
		
		replace time = -time
		foreach v in  workLW noagr_w agr_w self_noagr self_agr   {
			 g upper_b_par_`v'=  b_par_`v' +  1.96*se_par_`v'  
			 g lower_b_par_`v'=  b_par_`v' -  1.96*se_par_`v' 
			 
			 g upper_b_hour_`v'=  b_hour_`v' +  1.96*se_hour_`v'  
			 g lower_b_hour_`v'=  b_hour_`v' -  1.96*se_hour_`v'
			 
			 drop se_par_`v'  se_hour_`v' 
			 
			}
			*
			
			global tables  "${wd}tables/"
		
			label var time "Quarter before migration"
			
			label var  b_par_noagr_w "Non-agri. jobs" 
			label var b_par_agr_w  "Agri. jobs" 
			label var b_par_self_noagr  "Non-agri. self-employed" 
			label var b_par_self_agr "Agri. self-employed" 
			
			label var  b_hour_noagr_w "Non-agri. jobs" 
			label var b_hour_agr_w  "Agri. jobs" 
			label var b_hour_self_noagr  "Non-agri. self-employed" 
			label var b_hour_self_agr "Agri. self-employed"
			
			local i= 1
			foreach v in noagr_w agr_w self_noagr self_agr   {
			local l : variable label  b_par_`v'
			twoway (rcap upper_b_par_`v' lower_b_par_`v' time, color(black) ) ///
				(scatter b_par_`v' time , color(black) legend(off)) , title("`l'") saving(g`i', replace)  nodraw  
			
			local i = `i' +1 
			}
			
			gr combine g1.gph g2.gph g3.gph  g4.gph , cols(2) rows(2)
			graph export  "${tables}ENOE_pretrend_particip.png", width(1000) replace
			
			local i= 1
			foreach v in noagr_w agr_w self_noagr self_agr   {
			local l : variable label  b_par_`v'
			twoway (rcap upper_b_hour_`v' lower_b_hour_`v' time, color(black) ) ///
				(scatter b_hour_`v' time , color(black) legend(off)) , title("`l'") saving(g`i', replace)  nodraw  
			
			local i = `i' +1 
			}
			gr combine g1.gph g2.gph g3.gph  g4.gph , cols(2) rows(2)
			
		     
	
	
********************************************************************************	
**   PRE-TREND  TABLE A.1 
****************************************************************************************
		 
		use temp,clear 				
	
	cap drop __*
	
	global option varlabels  tex plain fragment  bdec(3)  se   starloc(1) starlevels(10 5  1) 
	
	global treat    HHUS_A3  HHUS_A2   HHUS_A1 
		
	
	outreg, clear(A) 
	outreg, clear(B) 	
	
	foreach v in workLW noagr_w agr_w self_noagr self_agr   {
	 
			local a : variable label  `v'
		
		qui su `v'   if age<75
		local av = round(r(mean), 0.001) 
		
		qui reghdfe `v'   ${treat}   if  age<75  , absorb(id_mun_period id_indiv ) vce (cluster id_household )
		outreg,  nod merge(A)  keep( ${treat}   )    ctitle(" ", "`v'")  ${option} addrows( "average outcome" , `av' )
		
		qui su  `v'_hour  if  age<75
		local av = round(r(mean), 0.001) 
		
		
		qui reghdfe `v'_hour    ${treat}    if  age<75  , absorb(id_mun_period id_indiv ) vce (cluster id_household )
		outreg,   nod merge(A)  keep( ${treat}   )    ctitle(" ", "hour `v' ")  ${option} addrows( "average outcome" , `av' )
		
	 }
	 *
	
	  
	 * labor income
		foreach v in  hhlabinc_percap labincome_month {
			qui su `v' if `v' > 0 , d
		    g C_`v' =  `v' 
		   replace C_`v'= . if  `v'>r(p95) 
		}
		
		outreg, clear(labinc) 
		qui su   C_labincome_month  if   age<75
		local av = round(r(mean), 0.001) 
		
	    qui reghdfe C_labincome_month    ${treat}   if  age<75  , absorb(id_mun_period id_indiv ) vce (cluster id_household )
		outreg,  nod merge(labinc)  keep( ${treat}   )    ctitle(" ", "indiv labincome")  ${option} addrows( "average outcome" , `av' ) 
	
	
		outreg,  replay(A)  keep( ${treat}    )     ${option}
		outreg,  replay(labinc)  keep( ${treat}    )     ${option}
		
		
